'''
Author: @xiaonancheng
Date: 2023-02-27 14:42:50
https://www.cnblogs.com/xiaonancheng
Copyright (c) 2023 by @xiaonancheng, All Rights Reserved. 
'''
from django.shortcuts import render,redirect
from .models import Doctor,Consult,Register,Department,Recipe
from UserApp.models import User,Userinfo,PEinfo,Visit,Health_doc,Relative
# from django.http import HttpResponse,JsonResponse

from django.http import HttpResponseRedirect,JsonResponse
def login_role_auth(fn):
    def wrapper(request,*args,**kwargs):
        if request.session.get('is_login', False) and request.session.get('user_type')=="/doctor" :
            return fn(request,*args,*kwargs)
        else:
            # 获取用户当前访问的url，并传递给/user/login/
            next = request.get_full_path() 
            request.session["is_login"]=False
            red = HttpResponseRedirect("/login/")
            return red
    return wrapper

@login_role_auth
def change_pwd(request):#个人中心 修改密码
    massage={"info":''}
    if request.method=="POST":
        old_pwd=request.POST.get("old_pwd")
        new_pwd=request.POST.get("new_pwd")
        if old_pwd==new_pwd:
            massage={"info":'新密码和原密码一致',"res":0}
        else:
            user=Doctor.objects.filter(uid=request.session.get('uid')).first()
            if user.password!=old_pwd:
                massage={"info":'原密码不正确',"res":1}
            else:
                user.password=new_pwd
                user.save()
                massage={"info":'密码修改成功',"res":2}
        return JsonResponse(massage) 
    else:
        return render(request,"doctor_passwd.html",massage)
def personal_info(request):#个人中心 个人信息
    if request.method=="GET":
        departments=Department.objects.all()
        doctor=Doctor.objects.filter(uid=request.session.get('uid')).first()
        return render(request,"doctor_index.html",{"user":doctor,'departments':departments})
    elif request.method=="POST":
        doctor_name=request.POST.get('doctor_name')
        uid=request.session.get('uid')
        email=request.POST.get('email')
        doctor_sex=request.POST.get('doctor_sex')
        doctor_age=request.POST.get('doctor_age')
        title=request.POST.get('title')
        hospital=request.POST.get('hospital')
        profile=request.POST.get('profile1')
        # role=request.POST.get('role')
        deid=request.POST.get('deid')
        good_at=request.POST.get('good_at')
        d=Doctor.objects.filter(uid=uid).first()
        d.doctor_name=doctor_name
        d.email=email
        d.doctor_sex=doctor_sex
        d.doctor_age=doctor_age
        d.title=title
        d.deid=Department.objects.filter(id=deid).first()
        d.hospital=hospital
        # d.role=role
        if profile:

            d.profile=profile
        d.good_at=good_at
        d.save()
        return redirect("/doctor/index/")
        

def online_consult(request):#线上咨询
    cid=request.GET.get('cid')
    type=request.GET.get('type')
    if request.method=="GET" and not cid:
        consults=Consult.objects.filter(d_id=request.session.get("uid"))
        return render(request,"online_manage.html",{'consults':consults})
    if request.method=="GET" and  cid and not type:
        # print(1)
        consult=Consult.objects.filter(c_id=cid).first()
        userinfo=Userinfo.objects.filter(uid=consult.u_id).first()
        return render(request,"treat_patient.html",{'consult':consult,'userinfo':userinfo})
    if request.method=="GET" and  cid and  type=="show":
        c=Consult.objects.filter(c_id=cid).first()
        return render(request,"consult_detail.html",{'c':c})
        # return render(request,"treat_patient.html",{'consult':consult,'userinfo':userinfo})
    if request.method=="POST":
        cid=request.POST.get('cid')
        consult=Consult.objects.filter(c_id=cid).first()
        diagnosisResult=request.POST.get('diagnosisResult')#诊断结果
        RP=request.POST.get('RP')#RP
        rp=Recipe()
        rp.Rp=RP
        rp.diagnose=diagnosisResult
        rp.symptom=''
        rp.save()
        consult.r_id=rp
        consult.status=1
        consult.save()
        return redirect("/doctor/online_consult/")

def register(request):#挂号/预约管理
    rid=request.GET.get('rid')
    if request.method=="GET" and not rid:
        registers=Register.objects.filter(d_id=request.session.get("uid"))
        return render(request,"register_manage.html",{'registers':registers})
    else:
        status=request.GET.get('status')
        register=Register.objects.filter(r_id=rid).first()
        register.status=status
        register.save()

        return redirect('/doctor/register/')

    
# def science(request):#个人中心 个人信息
#     pass